package com.csjbot.blackgaga.feature.navigation.setting;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.android.core.util.StrUtil;
import com.bumptech.glide.Glide;
import com.csjbot.blackgaga.R;
import com.csjbot.blackgaga.base.BaseFullScreenActivity;
import com.csjbot.blackgaga.base.BasePresenter;
import com.csjbot.blackgaga.core.RobotManager;
import com.csjbot.blackgaga.global.Constants;
import com.csjbot.blackgaga.localbean.NaviBean;
import com.csjbot.blackgaga.model.tcp.bean.Position;
import com.csjbot.blackgaga.router.BRouterPath;
import com.csjbot.blackgaga.util.BlackgagaLogger;
import com.csjbot.blackgaga.util.CheckOutUtil;
import com.csjbot.blackgaga.util.GsonUtils;
import com.csjbot.blackgaga.util.MD5;
import com.csjbot.blackgaga.util.SharedKey;
import com.csjbot.blackgaga.util.SharedPreUtil;
import com.csjbot.blackgaga.util.VolumeUtil;
import com.csjbot.blackgaga.widget.KeyPointView;
import com.csjbot.blackgaga.widget.NewRetailDialog;
import com.csjbot.blackgaga.widget.TitleView;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 孙秀艳 on 2017/11/2.
 * 导航导览设置界面
 */

@Route(path = BRouterPath.NAVI_SETTING)
public class NaviSettingActivity extends BaseFullScreenActivity implements NaviSettingContract.View, View.OnClickListener {
    NaviSettingContract.Presenter mPresenter;
    List<KeyPointView> keyPointViews;
    NaviType naviType = NaviType.NAVISTATE;
    NaviGuideListAdapter naviGuideListAdapter;
    List<NaviBean> naviBeanLists = new ArrayList<>();
    int index = -1;
    boolean isBack = false;

    @BindView(R.id.et_navi_name)
    EditText etNaviName;//导航点名称
    @BindView(R.id.et_navi_description)
    EditText etNaviDescription;//导航点描述
    @BindView(R.id.et_navi_xpoint)
    EditText etNaviXPoint;//x坐标
    @BindView(R.id.et_navi_ypoint)
    EditText etNaviYPoint;//y坐标
    @BindView(R.id.et_navi_zpoint)
    EditText etNaviZPoint;//z坐标
    @BindView(R.id.btn_get_position)
    Button btnGetPosition;//获取坐标点按钮
    @BindView(R.id.btn_yingbin_get_position)
    Button btnGetYingbinPosition;
    @BindView(R.id.et_yingbing_xpoint)
    EditText etYingbinXPoint;//迎宾点X坐标
    @BindView(R.id.et_yingbin_ypoint)
    EditText etYingbinYPoint;//迎宾点Y坐标
    @BindView(R.id.et_yingbin_zpoint)
    EditText etYingbinZPoint;//迎宾点Z坐标
    @BindView(R.id.btn_save)
    Button btnSave;//保存按钮
    @BindView(R.id.btn_delete)
    Button btnDelete;//删除按钮
    @BindView(R.id.rl_root)
    RelativeLayout rl_root;
    @BindView(R.id.btn_add_navi)
    Button btnAddNavi;//新增导航点
    @BindView(R.id.btn_add_yingbin)
    Button btnAddYingBin;//新增迎宾点
    @BindView(R.id.btn_add_guide)
    Button btnAddGuide;//一键导览
    @BindView(R.id.navi_tab_point)
    LinearLayout llNavi;//导航点数据布局
    @BindView(R.id.view_tab_yingbin)
    LinearLayout llYingBin;//迎宾点数据布局
    @BindView(R.id.view_tab_guide)
    LinearLayout llGuide;//一键导览数据布局
    @BindView(R.id.ll_save_btn)
    LinearLayout llSaveBtn;//保存按钮布局
    @BindView(R.id.ll_save_delete_btn)
    LinearLayout llSaveDeleteBtn;//保存和删除按钮布局
    @BindView(R.id.recyclerView_guide)
    RecyclerView recyclerView;//一键导览数据列表
    @BindView(R.id.btn_save_yingbin)
    Button btnSaveYingbin;
    @BindView(R.id.btn_map_upload)
    Button btnMapUpload;
    @BindView(R.id.iv_map)
    ImageView ivMap;
    @BindView(R.id.rl_map)
    RelativeLayout rl_map;
    /*中文、英文、数字但不包括下划线等符号*/
   private String REGEX_USERNAME = "^[\\u4E00-\\u9FA5A-Za-z0-9]+$";
    @Override
    protected BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_navi_setting;
    }

    @Override
    public void afterViewCreated(Bundle savedInstanceState) {
        super.afterViewCreated(savedInstanceState);
        boolean isGuideAll = getIntent().getBooleanExtra(Constants.GUIDE_ALL, false);
        if (isGuideAll) {
            onClick(btnAddGuide);
        }
    }

    @Override
    public void init() {
        super.init();
        CheckOutUtil.getInstance().verifyUseName(etNaviName, this,REGEX_USERNAME);
//        CheckOutUtil.getInstance().verifyUseName(etNaviDescription, this,REGEX_USERNAME);
        getTitleView().setBackVisibility(View.VISIBLE);
        mPresenter = new NaviSettingPresenter(this);
        mPresenter.initView(this);
        keyPointViews = new ArrayList<>();
        mPresenter.getNaviData();
        initListener();
        initRecycleView();
        back();
        initMap();
        speak(getString(R.string.navi_setting_guide_tip));
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    private void initMap() {
        String mapPath = SharedPreUtil.getString(SharedKey.NAVI_NAME, SharedKey.MAP_PATH);
        if (TextUtils.isEmpty(mapPath)) {// 获取不到地图，提示用户添加地图
//            mPresenter.uploadMap();
        } else {// 有地图，但是没设置
            Glide.with(this).load(mapPath).into(ivMap);
        }
    }

    private void initListener() {
        btnAddNavi.setOnClickListener(this);
        btnGetPosition.setOnClickListener(this);
        btnGetYingbinPosition.setOnClickListener(this);
        btnAddYingBin.setOnClickListener(this);
        btnSave.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnAddGuide.setOnClickListener(this);
        btnSaveYingbin.setOnClickListener(this);
        btnMapUpload.setOnClickListener(this);
    }

    private void initRecycleView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        naviGuideListAdapter = new NaviGuideListAdapter(this);
        recyclerView.setAdapter(naviGuideListAdapter);
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
    }

    @Override
    public boolean isOpenChat() {
        return true;
    }

    @Override
    public boolean isOpenTitle() {
        return true;
    }

    @Override
    public void showNaviMap(List<NaviBean> naviBeanList) {
        if (naviBeanList == null) {
            btnSaveYingbin.postDelayed(() -> {
                mPresenter.addNaviPoint();
            }, 500);
            return;
        }
        for (int i = 0; i < naviBeanList.size(); i++) {
            NaviBean naviBean = naviBeanList.get(i);
            BlackgagaLogger.debug("name:" + naviBean.getName());
            KeyPointView keyPointView = new KeyPointView(this);
            keyPointView.setIsTouch(true);//标注点可以触摸
            keyPointView.setIsBound(true);//标注点需要设置边界
            keyPointView.setBound(900, 450);
            keyPointView.layout(naviBean.left, naviBean.top, naviBean.right, naviBean.bottom);
            keyPointView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (naviType == NaviType.NAVISTATE) {
                        index = getIndex(naviBean);
                        showNaviData(naviBean);
                    } else if (naviType == NaviType.GUIDESTATE) {
                        if (StrUtil.isNotBlank(naviBean.getName())) {
                            naviBeanLists.add(naviBean);
                            naviGuideListAdapter.setGuideList(naviBeanLists);
                        } else {
                            Toast.makeText(NaviSettingActivity.this, R.string.navi_point_not_setting, Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
            keyPointView.setName(subString(naviBean.getName()));
//            moveView.setTextColor(Color.BLUE);
            keyPointView.setTag(naviBean);
            /* 设置平移坐标 */
            keyPointView.setTranslationX(naviBean.getTranslationX());
            keyPointView.setTranslationY(naviBean.getTranslationY());
            rl_map.addView(keyPointView);
            keyPointViews.add(keyPointView);
        }
        mPresenter.addNaviPoint();
    }

    private void showNaviData(NaviBean naviBean) {
        if (StrUtil.isBlank(naviBean.getName()) || StrUtil.isBlank(naviBean.getDescription())
                || naviBean.getPos() == null || StrUtil.isBlank(naviBean.getPos().getX())
                || StrUtil.isBlank(naviBean.getPos().getY()) || StrUtil.isBlank(naviBean.getPos().getRotation())) {
            clearNaviData();
            return;
        }
        etNaviName.setText(naviBean.getName());
        etNaviDescription.setText(naviBean.getDescription());
        etNaviXPoint.setText(naviBean.getPos().getX());
        etNaviYPoint.setText(naviBean.getPos().getY());
        etNaviZPoint.setText(naviBean.getPos().getRotation());
    }

    @Override
    public void addNaviPointItem(NaviBean naviBean) {
        KeyPointView keyPointView = new KeyPointView(this);
        keyPointView.setTag(naviBean);
        keyPointView.setName("");
        rl_map.addView(keyPointView);
        keyPointViews.add(keyPointView);
        keyPointView.setIsTouch(true);
        keyPointView.setIsBound(true);
        keyPointView.setBound(900, 450);
        index = keyPointViews.size() - 1;
        keyPointView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index = keyPointViews.size() - 1;
                clearNaviData();
            }
        });
        clearNaviData();
        if (isBack) {
            finish();
        }
    }

    @Override
    public void removeNaviItem(int position) {
        rl_map.removeView(keyPointViews.get(position));
        keyPointViews.remove(position);
        index = -1;
        clearNaviData();
    }

    @Override
    public void showYingbinData(Position position) {
        if (position != null) {
            etYingbinXPoint.setText(position.getX());
            etYingbinYPoint.setText(position.getY());
            etYingbinZPoint.setText(position.getRotation());
        }
    }

    @Override
    public void showGuideData(List<NaviBean> naviBeanList) {
        naviBeanLists.clear();
        if (naviBeanList != null && naviBeanList.size() > 0) {
            naviBeanLists.addAll(naviBeanList);
        }
        naviGuideListAdapter.setGuideList(naviBeanList);
    }

    @Override
    public void setNaviPosition(Position position) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (position != null && naviType == NaviType.NAVISTATE) {
                    etNaviXPoint.setText(position.getX());
                    etNaviYPoint.setText(position.getY());
                    etNaviZPoint.setText(position.getRotation());
                }
            }
        });
    }

    @Override
    public void setYingbinPosition(Position position) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (position != null && naviType == NaviType.YINGBINSTATE) {
                    etYingbinXPoint.setText(position.getX());
                    etYingbinYPoint.setText(position.getY());
                    etYingbinZPoint.setText(position.getRotation());
                }
            }
        });
    }

    @Override
    public void setMapFile(String path) {
        Glide.with(NaviSettingActivity.this).load(path).into(ivMap);
    }

    @Override
    public void addNotNaviPoint() {
        index = keyPointViews.size() - 1;
        clearNaviData();
        if (isBack) {
            finish();
        }
    }

    @Override
    public void saveSuccess() {
        Toast.makeText(this, R.string.save_success, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add_navi://添加导航点
                setTabTextAndBG(R.id.btn_add_navi);
                mPresenter.addNaviPoint();
                break;
            case R.id.btn_yingbin_get_position:
            case R.id.btn_get_position://获取坐标点
                if (RobotManager.getInstance().getConnectState()) {
                    mPresenter.getPosition();
                } else {
                    Toast.makeText(context, R.string.not_connect_slam, Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_add_yingbin://设置迎宾点
                setTabTextAndBG(R.id.btn_add_yingbin);
                mPresenter.getYingBinData();
                break;
            case R.id.btn_save://保存导航点数据
                if (naviType == NaviType.NAVISTATE) {
                    saveNaviData();
                } else if (naviType == NaviType.GUIDESTATE) {
                    saveGuideData();
                }
                break;
            case R.id.btn_delete://删除导航点数据
                if (naviType == NaviType.NAVISTATE) {
                    removeNaviData();
                } else if (naviType == NaviType.GUIDESTATE) {
                    removeGuideData();
                }
                break;
            case R.id.btn_add_guide://一键导览
                naviBeanLists.clear();
                setTabTextAndBG(R.id.btn_add_guide);
                mPresenter.getGuideData();
                break;
            case R.id.btn_save_yingbin:
            case R.id.ll_save_btn:
                saveYingBinData();
                break;
            case R.id.btn_map_upload://上传地图
                mPresenter.uploadMap();
                break;
            default:
                break;
        }
    }

    private void saveGuideData() {
        if (naviBeanLists == null || naviBeanLists.size() <= 0) {
            isBack = false;
            Toast.makeText(this, R.string.plz_choose_guide_data, Toast.LENGTH_LONG).show();
            return;
        }
        mPresenter.saveGuideData(naviBeanLists);
        if (isBack) {
            finish();
        }
    }

    /**
     * 保存迎宾数据
     */
    private void saveYingBinData() {
        String msg = yingBinValidate();
        if (msg != null) {
            isBack = false;
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
            return;
        }
        Position position = new Position();
        position.setX(etYingbinXPoint.getText().toString());
        position.setY(etYingbinYPoint.getText().toString());
        position.setRotation(etYingbinZPoint.getText().toString());
        mPresenter.saveYingBinPoint(position);
        if (isBack) {
            finish();
        }
    }

    /**
     * 迎宾数据校验
     */
    private String yingBinValidate() {
        String msg = null;
        if (StrUtil.isBlank(etYingbinXPoint.getText().toString())) {
            msg = getString(R.string.plz_input_x_point);
        } else if (StrUtil.isBlank(etYingbinYPoint.getText().toString())) {
            msg = getString(R.string.plz_input_y_point);
        } else if (StrUtil.isBlank(etYingbinZPoint.getText().toString())) {
            msg = getString(R.string.plz_input_z_point);
        }
        return msg;
    }

    /**
     * 保存导航点数据
     */
    private void saveNaviData() {
        String msg = naviValidate();
        if (msg != null) {
            isBack = false;
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
            return;
        }
        if (index == -1) {
            Toast.makeText(this, R.string.plz_choose_save_navi_data, Toast.LENGTH_LONG).show();
            return;
        }
        if (keyPointViews != null && keyPointViews.size() > 0) {
            KeyPointView keyPointView = keyPointViews.get(index);
            NaviBean naviBeam = null;
            if (keyPointView.getTag() == null) {
                naviBeam = new NaviBean();
                naviBeam.setId(MD5.getRandomString(10));
            } else {
                naviBeam = (NaviBean) keyPointView.getTag();
            }
            naviBeam.setName(etNaviName.getText().toString());//非空判断
            naviBeam.setDescription(etNaviDescription.getText().toString());
            Position position = new Position();
            position.setX(etNaviXPoint.getText().toString());
            position.setY(etNaviYPoint.getText().toString());
            position.setRotation(etNaviZPoint.getText().toString());
            naviBeam.setPos(position);
            naviBeam.setTranslationX(keyPointView.getTranslationX());
            naviBeam.setTranslationY(keyPointView.getTranslationY());
            naviBeam.left = keyPointView.getLeft();
            naviBeam.right = keyPointView.getRight();
            naviBeam.top = keyPointView.getTop();
            naviBeam.bottom = keyPointView.getBottom();
            BlackgagaLogger.debug("left=" + keyPointView.getLeft() + " right=" + keyPointView.getRight() + " " +
                    "top=" + keyPointView.getTop() + " bottom=" + keyPointView.getBottom());
            keyPointView.setTag(naviBeam);
            keyPointView.setName(subString(etNaviName.getText().toString()));
            keyPointView.setIsTouch(true);
            keyPointView.setIsBound(true);
            keyPointView.setBound(900, 450);
            keyPointView.layout(naviBeam.left, naviBeam.top, naviBeam.right, naviBeam.bottom);
            final NaviBean naviBean = naviBeam;
            keyPointView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (naviType == NaviType.NAVISTATE) {
                        index = getIndex(naviBean);
                        showNaviData(naviBean);
                    } else if (naviType == NaviType.GUIDESTATE) {
                        if (StrUtil.isNotBlank(naviBean.getName())) {
                            naviBeanLists.add(naviBean);
                            naviGuideListAdapter.setGuideList(naviBeanLists);
                        } else {
                            Toast.makeText(NaviSettingActivity.this, R.string.navi_point_not_setting, Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
            mPresenter.saveNaviPointByPosition(naviBean, index);
        }
    }

    private int getIndex(NaviBean naviBean) {
        int position = 0;
        String json = SharedPreUtil.getString(SharedKey.NAVI_NAME, SharedKey.NAVI_KEY);
        List<NaviBean> naviList = GsonUtils.jsonToObject(json, new TypeToken<List<NaviBean>>() {
        }.getType());
        if (naviList != null && naviList.size() > 0) {
            for (int i = 0; i < naviList.size(); i++) {
                NaviBean bean = naviList.get(i);
                if (bean.getTranslationX() == naviBean.getTranslationX() && bean.getTranslationY() == naviBean.getTranslationY()) {
                    position = i;
                }
            }
        }
        return position;
    }

    private String naviValidate() {
        String msg = null;
        if (StrUtil.isBlank(etNaviName.getText().toString())) {
            msg = getString(R.string.plz_input_nave_name);
        } else if (StrUtil.isBlank(etNaviDescription.getText().toString())) {
            msg = getString(R.string.plz_input_navi_description);
        } else if (StrUtil.isBlank(etNaviXPoint.getText().toString())) {
            msg = getString(R.string.plz_input_x_point);
        } else if (StrUtil.isBlank(etNaviYPoint.getText().toString())) {
            msg = getString(R.string.plz_input_y_point);
        } else if (StrUtil.isBlank(etNaviZPoint.getText().toString())) {
            msg = getString(R.string.plz_input_z_point);
        }
        return msg;
    }

    private void removeGuideData() {
        if (naviBeanLists == null || naviBeanLists.size() <= 0) {
            Toast.makeText(this, R.string.no_navi_data_delete, Toast.LENGTH_LONG).show();
            return;
        }
        showNewRetailDialog(getString(R.string.delete), getString(R.string.is_delete_navi_data), new NewRetailDialog.OnDialogClickListener() {
            @Override
            public void yes() {
                refreshGuideView();
                dismissNewRetailDialog();
            }

            @Override
            public void no() {
                dismissNewRetailDialog();
            }
        });
    }

    private void refreshGuideView() {
        naviBeanLists.clear();
        mPresenter.saveGuideData(naviBeanLists);
        naviGuideListAdapter.setGuideList(naviBeanLists);
    }

    /**
     * 删除导航点数据
     */
    private void removeNaviData() {
        if (keyPointViews == null || keyPointViews.size() <= 0) {
            Toast.makeText(this, R.string.no_navi_data_delete, Toast.LENGTH_LONG).show();
            return;
        }
        if (index == -1) {
            Toast.makeText(this, R.string.plz_choose_navi_data, Toast.LENGTH_LONG).show();
            return;
        }
        int msdId = R.string.is_delete_navi_data;
        if (mPresenter.isGuideData(index)) {
            msdId = R.string.is_delete_using_navi_data;
        }
        showNewRetailDialog(getString(R.string.delete), getString(msdId), new NewRetailDialog.OnDialogClickListener() {
            @Override
            public void yes() {
                mPresenter.removeNaviPointByPosition(index);
                dismissNewRetailDialog();
            }

            @Override
            public void no() {
                dismissNewRetailDialog();
            }
        });
    }

    private void clearNaviData() {
        etNaviName.setText("");
        etNaviDescription.setText("");
        etNaviXPoint.setText("");
        etNaviYPoint.setText("");
        etNaviZPoint.setText("");
    }

    private void setTabTextAndBG(int id) {
        if (id == R.id.btn_add_navi) {
            naviType = NaviType.NAVISTATE;
            btnAddNavi.setBackgroundResource(R.drawable.navi_tab_point_select);
            btnAddYingBin.setBackgroundResource(R.drawable.navi_tab_yingbin);
            btnAddGuide.setBackgroundResource(R.drawable.navi_tab_guide);
            btnAddNavi.setTextColor(getResources().getColor(R.color.gray333333));
            btnAddYingBin.setTextColor(getResources().getColor(R.color.gray666666));
            btnAddGuide.setTextColor(getResources().getColor(R.color.gray666666));
            llNavi.setVisibility(View.VISIBLE);
            llYingBin.setVisibility(View.GONE);
            llGuide.setVisibility(View.GONE);
            llSaveBtn.setVisibility(View.GONE);
            llSaveDeleteBtn.setVisibility(View.VISIBLE);
        } else if (id == R.id.btn_add_yingbin) {
            naviType = NaviType.YINGBINSTATE;
            btnAddNavi.setBackgroundResource(R.drawable.navi_tab_point);
            btnAddYingBin.setBackgroundResource(R.drawable.navi_tab_yingbin_select);
            btnAddGuide.setBackgroundResource(R.drawable.navi_tab_guide);
            btnAddNavi.setTextColor(getResources().getColor(R.color.gray666666));
            btnAddYingBin.setTextColor(getResources().getColor(R.color.gray333333));
            btnAddGuide.setTextColor(getResources().getColor(R.color.gray666666));
            llNavi.setVisibility(View.GONE);
            llYingBin.setVisibility(View.VISIBLE);
            llGuide.setVisibility(View.GONE);
            llSaveBtn.setVisibility(View.VISIBLE);
            llSaveDeleteBtn.setVisibility(View.GONE);
        } else if (id == R.id.btn_add_guide) {
            naviType = NaviType.GUIDESTATE;
            btnAddNavi.setBackgroundResource(R.drawable.navi_tab_point);
            btnAddYingBin.setBackgroundResource(R.drawable.navi_tab_yingbin);
            btnAddGuide.setBackgroundResource(R.drawable.navi_tab_guide_select);
            btnAddNavi.setTextColor(getResources().getColor(R.color.gray666666));
            btnAddYingBin.setTextColor(getResources().getColor(R.color.gray666666));
            btnAddGuide.setTextColor(getResources().getColor(R.color.gray333333));
            llNavi.setVisibility(View.GONE);
            llYingBin.setVisibility(View.GONE);
            llGuide.setVisibility(View.VISIBLE);
            llSaveBtn.setVisibility(View.GONE);
            llSaveDeleteBtn.setVisibility(View.VISIBLE);
        }
    }

    private void back() {
        getTitleView().setBackListener(new TitleView.OnBackListener() {
            @Override
            public void onClick() {
                onClickBack();
            }
        });
    }

    private void onClickBack() {
        isBack = true;
        if (naviType == NaviType.NAVISTATE) {
            if (StrUtil.isNotBlank(etNaviName.getText().toString()) || StrUtil.isNotBlank(etNaviDescription.getText().toString())
                    || StrUtil.isNotBlank(etNaviXPoint.getText().toString()) || StrUtil.isNotBlank(etNaviYPoint.getText().toString())
                    || StrUtil.isNotBlank(etNaviZPoint.getText().toString())) {
                SaveNaviDialog();
            } else {
                finish();
            }
        } else if (naviType == NaviType.YINGBINSTATE) {
            String json = SharedPreUtil.getString(SharedKey.YINGBIN_NAME, SharedKey.YINGBIN_KEY);
            List<Position> positionList = GsonUtils.jsonToObject(json, new TypeToken<List<Position>>() {
            }.getType());

            if ((StrUtil.isNotBlank(etYingbinXPoint.getText().toString()) || StrUtil.isNotBlank(etYingbinYPoint.getText().toString())
                    || StrUtil.isNotBlank(etYingbinZPoint.getText().toString()))
                    && (positionList == null || !positionList.get(0).getX().equals(etYingbinXPoint.getText().toString())
                    || !positionList.get(0).getY().equals(etYingbinYPoint.getText().toString())
                    || !positionList.get(0).getRotation().equals(etYingbinZPoint.getText().toString()))) {
                saveYingBinDialog();
            } else {
                finish();
            }
        } else if (naviType == NaviType.GUIDESTATE) {
            String json = SharedPreUtil.getString(SharedKey.GUIDE_NAME, SharedKey.GUIDE_KEY);
            List<NaviBean> guideLists = GsonUtils.jsonToObject(json, new TypeToken<List<NaviBean>>() {
            }.getType());
            if (naviBeanLists != null && naviBeanLists.size() > 0 && (guideLists == null || guideLists.size() != naviBeanLists.size())) {
                saveGuideDialog();
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    private void saveGuideDialog() {
        showNewRetailDialog(getString(R.string.save), getString(R.string.save_guide_tip), new NewRetailDialog.OnDialogClickListener() {
            @Override
            public void yes() {
                saveGuideData();
                dismissNewRetailDialog();
            }

            @Override
            public void no() {
                finish();
                dismissNewRetailDialog();
            }
        });
    }

    private void saveYingBinDialog() {
        showNewRetailDialog(getString(R.string.save), getString(R.string.save_yingbin_tip), new NewRetailDialog.OnDialogClickListener() {
            @Override
            public void yes() {
                saveYingBinData();
                dismissNewRetailDialog();
            }

            @Override
            public void no() {
                finish();
                dismissNewRetailDialog();
            }
        });
    }

    private void SaveNaviDialog() {
        showNewRetailDialog(getString(R.string.save), getString(R.string.save_navi_tip), new NewRetailDialog.OnDialogClickListener() {
            @Override
            public void yes() {
                saveNaviData();
                dismissNewRetailDialog();
            }

            @Override
            public void no() {
                finish();
                dismissNewRetailDialog();
            }
        });
    }

    private String subString(String name) {
        String strName;
        if (name != null && !name.trim().equals("")) {
            if (name.length() > 5) {
                strName = name.substring(0, 4) + "...";
            } else {
                strName = name;
            }
        } else {
            strName = "";
        }
        return strName;
    }

    @Override
    protected void onPause() {
        if (isBack) {
            mPresenter.saveNaviData();
        }
        super.onPause();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                onClickBack();
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
